package com.woniuxy.stage3demo.mapper.chenyuxuanMapper;

import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.DeptBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.StaffBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;

/**
 * 员工SQL注解mapper类
 */
@Mapper
public interface StaffMapper {
    /**
     * 根据部门id查询部门所有人数
     * @param id
     * @return
     */
   /* @Select("select count(id) from staff where dept_id=#{id}")
    int countDeptStaffnum(@Param("id") int id);*/

    /**
     * 添加员工信息
     *
     * @param staffBean
     * @return
     */
    @Options(
            useGeneratedKeys = true,//主键自增
            keyProperty = "s.id"
    )
    @Insert("insert into staff " +
            "(login_name,password,name,gender,telphone,email,dept_id,stuff_position,stuff_num,address,graduate_school,graduate_time,top_edu,major,note,create_time,use_status,last_login_time,del_flag)" +
            " values(#{s.loginName},#{s.password},#{s.name},#{s.gender},#{s.telphone},#{s.email},#{s.deptBean.id},#{s.position},#{s.stuffNum},#{s.address},#{s.graduateSchool},#{s.graduateTime},#{s.topEdu},#{s.major},#{s.note},#{s.createTime},#{s.useStatus},#{s.lastLoginTime},#{s.delFlag})")
    int addStaffInDept(@Param("s") StaffBean staffBean);


    /**
     * 添加员工信息
     *
     * @param staffBean
     * @return
     */
    @Options(
            useGeneratedKeys = true,//主键自增
            keyProperty = "s.id"
    )
    @Insert("insert into staff " +
            "(login_name,password,name,gender,telphone,email,dept_id,stuff_position,stuff_num,address,graduate_school,graduate_time,top_edu,major,note,create_time,use_status,last_login_time,del_flag)" +
            " values(#{s.loginName},#{s.password},#{s.name},#{s.gender},#{s.telphone},#{s.email},#{d.id},#{s.position},#{s.stuffNum},#{s.address},#{s.graduateSchool},#{s.graduateTime},#{s.topEdu},#{s.major},#{s.note},#{s.createTime},#{s.useStatus},#{s.lastLoginTime},#{s.delFlag})")
    int addStaff(@Param("s") StaffBean staffBean, @Param("d") DeptBean deptBean);

    /**
     * 删除员工
     *
     * @param id
     */
    @Update("update staff set del_flag = 1 where id = #{id}")
    int deleteStaff(@Param("id") int id);

    /**
     * 根据id查询员工信息
     *
     * @param id
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "loginName", column = "login_name", javaType = String.class),
            @Result(property = "password", column = "password", javaType = String.class),
            @Result(property = "name", column = "name", javaType = String.class),
            @Result(property = "gender", column = "gender", javaType = int.class),
            @Result(property = "telphone", column = "telphone", javaType = String.class),
            @Result(property = "email", column = "email", javaType = String.class),
            @Result(property = "deptBean", column = "dept_id", javaType = DeptBean.class, one =
            @One(select = "com.woniuxy.stage3demo.mapper.chenyuxuanMapper.DeptMapper.findOneById", fetchType = FetchType.LAZY)),
            @Result(property = "position", column = "stuff_position", javaType = String.class),
            @Result(property = "stuffNum", column = "stuff_num", javaType = String.class),
            @Result(property = "address", column = "address", javaType = String.class),
            @Result(property = "graduateSchool", column = "graduate_school", javaType = String.class),
            @Result(property = "graduateTime", column = "graduate_time", javaType = Date.class),
            @Result(property = "topEdu", column = "top_edu", javaType = int.class),
            @Result(property = "major", column = "major", javaType = String.class),
            @Result(property = "note", column = "note", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "useStatus", column = "use_status", javaType = int.class),
            @Result(property = "lastLoginTime", column = "last_login_time", javaType = Date.class),
            @Result(property = "delFlag", column = "del_flag", javaType = long.class)
    })
    @Select("select * from staff where id=#{id}")
    StaffBean findStaffById(int id);


    /**
     * 员工分页查询
     *
     * @param request   分页条件请求参数
     * @param staffBean 员工信息
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "loginName", column = "login_name", javaType = String.class),
            @Result(property = "password", column = "password", javaType = String.class),
            @Result(property = "name", column = "name", javaType = String.class),
            @Result(property = "gender", column = "gender", javaType = int.class),
            @Result(property = "telphone", column = "telphone", javaType = String.class),
            @Result(property = "email", column = "email", javaType = String.class),
            @Result(property = "deptBean", column = "dept_id", javaType = DeptBean.class, one =
            @One(select = "com.woniuxy.stage3demo.mapper.chenyuxuanMapper.DeptMapper.findOneById", fetchType = FetchType.LAZY)),
            @Result(property = "position", column = "stuff_position", javaType = String.class),
            @Result(property = "stuffNum", column = "stuff_num", javaType = String.class),
            @Result(property = "address", column = "address", javaType = String.class),
            @Result(property = "graduateSchool", column = "graduate_school", javaType = String.class),
            @Result(property = "graduateTime", column = "graduate_time", javaType = Date.class),
            @Result(property = "topEdu", column = "top_edu", javaType = int.class),
            @Result(property = "major", column = "major", javaType = String.class),
            @Result(property = "note", column = "note", javaType = String.class),
            @Result(property = "createTime", column = "create_time", javaType = Date.class),
            @Result(property = "useStatus", column = "use_status", javaType = int.class),
            @Result(property = "lastLoginTime", column = "last_login_time", javaType = Date.class),
            @Result(property = "delFlag", column = "del_flag", javaType = long.class)
    })
    @Select("select s.* ,d.dept_name from staff as s ,dept as d where s.dept_id=d.id and s.del_flag = 0 order by ${p.sort} ${p.sortOrder}")
    List<StaffBean> findAllByParams(@Param("p") PageRequest request, @Param("staff") StaffBean staffBean);

    /**
     * 修改员工信息
     *
     * @param staffBean
     */
    @Update("<script>" +
            "update staff" +
            "<set>" +
            "<if test='s.name != null'>" +
            " name = #{s.name}," +
            "</if>" +
            "<if test='s.loginName != null'>" +
            " login_name = #{s.loginName}," +
            "</if>" +
            "<if test='s.password != null'>" +
            " password = #{s.password}," +
            "</if>" +
            "<if test='s.gender != null'>" +
            " gender = #{s.gender}," +
            "</if>" +
            "<if test='s.telphone != null'>" +
            " telphone = #{s.telphone}," +
            "</if>" +
            "<if test='s.email != null'>" +
            " email = #{s.email}," +
            "</if>" +
            "<if test='s.deptBean!=null and s.deptBean.id != null'>" +
            " dept_id = #{s.deptBean.id}," +
            "</if>" +
            "<if test='s.position != null'>" +
            " stuff_position = #{s.position}," +
            "</if>" +
            "<if test='s.stuffNum != null'>" +
            " stuff_num = #{s.stuffNum}," +
            "</if>" +
            "<if test='s.address != null'>" +
            " address = #{s.address}," +
            "</if>" +
            "<if test='s.graduateSchool != null'>" +
            " graduate_school = #{s.graduateSchool}," +
            "</if>" +
            "<if test='s.graduateTime != null'>" +
            " graduate_time = #{s.graduateTime}," +
            "</if>" +
            "<if test='s.topEdu != null'>" +
            " top_edu = #{s.topEdu}," +
            "</if>" +
            "<if test='s.major != null'>" +
            " major = #{s.major}," +
            "</if>" +
            "<if test='s.note != null'>" +
            " note = #{s.note}," +
            "</if>" +
            "<if test='s.createTime != null'>" +
            " create_time = #{s.createTime}," +
            "</if>" +
            "<if test='s.useStatus != null'>" +
            " use_status = #{s.useStatus}," +
            "</if>" +
            "<if test='s.lastLoginTime != null'>" +
            " last_login_time = #{s.lastLoginTime}," +
            "</if>" +
            "<if test='s.delFlag != null'>" +
            " del_flag = #{s.delFlag}," +
            "</if>" +
            "</set>" +
            "where id = #{s.id}" +
            "</script>")
    int updateStaff(@Param("s") StaffBean staffBean);

    /**
     * 根据部门ID查询部门所有成员
     * @param id
     * @return
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "name", column = "name", javaType = String.class),
    })
    @Select("select id,name from staff where dept_id = #{id}")
    List<StaffBean> findAllStaffByDeptId(@Param("id") int id);
}
